package Control;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.*;
import java.util.Date;
import Database.*;
import Model.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import oracle.net.jdbc.TNSAddress.AddressList;

public class ManagerGUI extends JFrame implements ActionListener
{
        private JButton productAdd, productDel, productUpdate, userAdd, userUpdate,
                        userDel, custAdd, custUpdate, custDel, suppAdd, suppUpdate,
                        suppDel, logout, viewProfit, popItems, stockOrder, viewProd;
        private JTextField username;
        private JPanel logo, rightPanel, leftPanel, productsPanel, usersPanel,
                        loyaltyPanel, supplierPanel;
        private TableOperations to;
        private JLabel logoPicture;
        private ImageIcon icon = new ImageIcon("frameIcon.png");

        public ManagerGUI(String managerUsername, final TableOperations to)
        {
                GridBagLayout layout = new GridBagLayout();
                GridBagConstraints gc = new GridBagConstraints();

                setIconImage(icon.getImage());
                setTitle("MDL - Manager");
                setSize(750, 500);
                setLocationRelativeTo(null);
                setResizable(false);
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                this.setLayout(layout);
                this.to = to;
               
                addWindowListener(new java.awt.event.WindowAdapter() {
                                @Override
                                public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                                		to.close();
                                        LoginGUI lg = new LoginGUI();
                                        lg.setVisible(true);
                                }
                        });

                Insets insets = new Insets(14, 0, 14, 150);
                // Logo Panel
                BufferedImage myPicture = null;
                try
                {
                        myPicture = ImageIO.read(new File("logo.png"));
                } catch (IOException e)
                {
                        e.printStackTrace();
                }
                logo = new JPanel();
                logoPicture = new JLabel(new ImageIcon(myPicture));
                logo.add(logoPicture);

                gc.gridx = 2;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 2;
                gc.insets = insets;
                gc.fill = GridBagConstraints.WEST;
                this.add(logo, gc);

                insets = new Insets(0, 3, 0, 3);

                // Right Panel
                rightPanel = new JPanel();
                rightPanel.setLayout(layout);

                gc.gridx = 1;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                rightPanel.add(new JLabel("Welcome"), gc);

                insets = new Insets(0, 3, 14, 3);

                Font f = new Font("Arial", Font.BOLD, 12);
                username = new JTextField(27);
                username.setMinimumSize(new Dimension(140, 25));
                username.setFont(f);
                username.setHorizontalAlignment(JTextField.CENTER);
                username.setText(managerUsername);
                username.setEditable(false);
                gc.gridx = 1;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                rightPanel.add(username, gc);

                insets = new Insets(14, 3, 14, 3);

                logout = new JButton("Logout");
                logout.setPreferredSize(new Dimension(150, 100));
                logout.addActionListener(this);
                gc.gridx = 1;
                gc.gridy = 2;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.fill = GridBagConstraints.BOTH;
                gc.insets = insets;

                rightPanel.add(logout, gc);

                viewProfit = new JButton("View Profit");
                viewProfit.addActionListener(this);
                gc.gridx = 1;
                gc.gridy = 4;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                rightPanel.add(viewProfit, gc);

                popItems = new JButton("View Popular Items");
                popItems.addActionListener(this);
                gc.gridx = 1;
                gc.gridy = 5;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                rightPanel.add(popItems, gc);

                stockOrder = new JButton(" View Stock Order  ");
                stockOrder.addActionListener(this);
                gc.gridx = 1;
                gc.gridy = 6;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                rightPanel.add(stockOrder, gc);

                viewProd = new JButton("View Products");
                viewProd.addActionListener(this);
                gc.gridx = 1;
                gc.gridy = 7;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                rightPanel.add(viewProd, gc);

                gc.gridx = 1;
                gc.gridy = 2;
                gc.weighty = 2;
                gc.weightx = 1;
                this.add(rightPanel, gc);

                insets = new Insets(5, 50, 13, 50);
                // Left Panel
                leftPanel = new JPanel();
                leftPanel.setLayout(layout);

                // Products Panel
                productsPanel = new JPanel();
                productsPanel.setBorder(BorderFactory.createTitledBorder("Products"));
                productsPanel.setLayout(layout);

                productAdd = new JButton("Add");
                gc.gridx = 1;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                productsPanel.add(productAdd, gc);
                productAdd.addActionListener(this);

                productUpdate = new JButton("Update");
                gc.gridx = 3;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                productsPanel.add(productUpdate, gc);
                productUpdate.addActionListener(this);

                productDel = new JButton("Delete");
                gc.gridx = 5;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                productsPanel.add(productDel, gc);
                productDel.addActionListener(this);

                // Users Panel
                usersPanel = new JPanel();
                usersPanel.setBorder(BorderFactory.createTitledBorder("Users"));
                usersPanel.setLayout(layout);

                userAdd = new JButton("Add");
                gc.gridx = 1;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                usersPanel.add(userAdd, gc);
                userAdd.addActionListener(this);

                userUpdate = new JButton("Update");
                gc.gridx = 3;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                usersPanel.add(userUpdate, gc);
                userUpdate.addActionListener(this);

                userDel = new JButton("Delete");
                gc.gridx = 5;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                usersPanel.add(userDel, gc);
                userDel.addActionListener(this);

                // LoyaltyCustomer Panel
                loyaltyPanel = new JPanel();
                loyaltyPanel.setBorder(BorderFactory
                                .createTitledBorder("Loyalty Customer"));
                loyaltyPanel.setLayout(layout);

                custAdd = new JButton("Add");
                gc.gridx = 1;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                loyaltyPanel.add(custAdd, gc);
                custAdd.addActionListener(this);

                custUpdate = new JButton("Update");
                gc.gridx = 3;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                loyaltyPanel.add(custUpdate, gc);
                custUpdate.addActionListener(this);

                custDel = new JButton("Delete");
                gc.gridx = 5;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                loyaltyPanel.add(custDel, gc);
                custDel.addActionListener(this);

                // Supplier Panel
                supplierPanel = new JPanel();
                supplierPanel.setBorder(BorderFactory.createTitledBorder("Supplier"));
                supplierPanel.setLayout(layout);

                suppAdd = new JButton("Add");
                gc.gridx = 1;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                supplierPanel.add(suppAdd, gc);
                suppAdd.addActionListener(this);

                suppUpdate = new JButton("Update");
                gc.gridx = 3;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                supplierPanel.add(suppUpdate, gc);
                suppUpdate.addActionListener(this);

                suppDel = new JButton("Delete");
                gc.gridx = 5;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                gc.insets = insets;
                supplierPanel.add(suppDel, gc);
                suppDel.addActionListener(this);

                insets = new Insets(10, 20, 10, 20);

                gc.gridx = 0;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(productsPanel, gc);

                gc.gridx = 0;
                gc.gridy = 3;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(usersPanel, gc);

                gc.gridx = 0;
                gc.gridy = 5;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(loyaltyPanel, gc);

                gc.gridx = 0;
                gc.gridy = 7;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(supplierPanel, gc);

                gc.gridx = 2;
                gc.gridy = 2;
                gc.weighty = 2;
                gc.weightx = 2;
                this.add(leftPanel, gc);

        }

        public void actionPerformed(ActionEvent e)
        {

                // logout, viewProfit, popItems, stockOrder
                if (e.getSource() == productAdd)
                {
                        AddProductGUI addPG = new AddProductGUI();
                        addPG.setVisible(true);
                }
                else if (e.getSource() == productUpdate)
                {
                        EditProduct ep = new EditProduct();
                        ep.setVisible(true);
                }
                else if (e.getSource() == productDel)
                {
                        DeleteProduct dp = new DeleteProduct();
                        dp.setVisible(true);
                }
                else if (e.getSource() == userAdd)
                {
                        AddUserGUI addUG = new AddUserGUI();
                        addUG.setVisible(true);
                }
                else if (e.getSource() == userUpdate)
                {
                        EditUser user = new EditUser();
                        user.setVisible(true);
                }
                else if (e.getSource() == userDel)
                {
                        DeleteUser du = new DeleteUser();
                        du.setVisible(true);
                }
                else if (e.getSource() == custAdd)
                {
                        AddCustomer ac = new AddCustomer();
                        ac.setVisible(true);
                }
                else if (e.getSource() == custUpdate)
                {
                        EditLoyaltyCust cust = new EditLoyaltyCust();
                        cust.setVisible(true);
                }
                else if (e.getSource() == custDel)
                {
                        DeleteCustomer dc = new DeleteCustomer();
                        dc.setVisible(true);
                }
                else if (e.getSource() == suppAdd)
                {
                        AddSupplier as = new AddSupplier();
                        as.setVisible(true);
                }
                else if (e.getSource() == suppUpdate)
                {
                        EditSupplier es = new EditSupplier();
                        es.setVisible(true);
                }
                else if (e.getSource() == suppDel)
                {
                        DeleteSupplier ds = new DeleteSupplier();
                        ds.setVisible(true);
                }
                else if (e.getSource() == logout)
                {                
                                to.close();
                        LoginGUI lGui = new LoginGUI();
                        lGui.setVisible(true);
                        this.dispose();
                }
                else if (e.getSource() == viewProd)
                {
                        ProductView pv = new ProductView();
                        pv.setVisible(true);
                }
                else if(e.getSource() == viewProfit)
                {
                        ViewProfit vp = new ViewProfit(to.CalculateProfit());
                        vp.setVisible(true);
                }
                else if(e.getSource() == stockOrder)
                {
                        StockOrder so = new StockOrder(to.stockOrder());
                        so.setVisible(true);
                }
                else if(e.getSource() == popItems)
                {
                        PopularItem pi = new PopularItem(to.getPopular());
                        pi.setVisible(true);
                }

        }
}
